<?php

namespace app\modules\api\controllers;

use app\models\UploadFile;
use app\modules\api\CommonController;

/**
 * Default controller for the `api` module
 */
class UploadController extends CommonController
{
    //上传文件
    public function actionWx()
    {
        try{
            $obj = UploadFile::addToOssByUpload('file',UploadFile::PATH_USER);
            return $this->success(['id'=>$obj->id,'url'=>$obj->getFileUrl()]);
        }catch (\Exception $e){
            return $this->error($e);
        }
    }
    //详情
    public function actionInfo()
    {
        try{
            $id = Yii::$app->request->post('game_id', 1);
            $obj = Game::findOne($id);
            if(empty($obj)) $obj = new Game();
            $row = $obj->getInfoData();
            $row['player_nums_arr'] = Game::$player_nums_arr;
            $row['system_arr'] = Game::$system_arr;
            return $this->success($row);
        }catch (\Exception $e){
            return $this->error($e);
        }
    }
    //参赛球队
    public function actionTeams()
    {
        try{
            $game_id = Yii::$app->request->post('game_id', 1);
//            $game = Game::findOne($game_id);
//            if(empty($game)) throw new \Exception('赛事不存在', 1);
            $game_teams = GameTeam::find()->where(['game_id'=>$game_id])->all();
            $rows = [];
            foreach ($game_teams as $game_team) {
                $rows[] = $game_team->team->getListData();
            }
            return $this->success($rows);
        }catch (\Exception $e){
            return $this->error($e);
        }
    }
    //积分榜
    public function actionTeamRanking()
    {
        try{
            $game_id = Yii::$app->request->post('game_id', 1);
            $game = Game::findOne($game_id);
            if(empty($game)) throw new \Exception('赛事不存在', 1);
            $query = GameTeamRanking::find()->where(['game_id'=>$game_id]);
            $query->with(['team']);
            $query->orderBy("rank DESC");
            $objs = $query->all();
            $rows = [];
            foreach ($objs as $obj) {
                $rows[] = $obj->getListData();
            }
            return $this->success(['rows'=>$rows]);
        }catch (\Exception $e){
            return $this->error($e);
        }
    }
    //球员榜
    public function actionPlayerRanking()
    {
        try{
            $game_id = Yii::$app->request->post('game_id', 1);
            $game = Game::findOne($game_id);
            if(empty($game)) throw new \Exception('赛事不存在', 1);
            $query = GamePlayerRanking::find()->where(['game_id'=>$game_id]);
            $query->with(['team','player']);
            $query->orderBy("gs DESC");
            $objs = $query->all();
            $rows = [];
            foreach ($objs as $obj) {
                $rows[] = $obj->getListData();
            }
            return $this->success(['rows'=>$rows]);
        }catch (\Exception $e){
            return $this->error($e);
        }
    }
    //保存信息
    public function actionSave()
    {
        try{
            $id = Yii::$app->request->post('game_id', 1);
            $title = Yii::$app->request->post('title', '');
            if(empty($title)) throw new \Exception('名称不能为空',1);
            $cover_id = Yii::$app->request->post('cover_id', 0)?:0;
            if(empty($cover_id)) throw new \Exception('logo不能为空',1);
            $intro = Yii::$app->request->post('intro', '');
            if(empty($intro)) throw new \Exception('简介不能为空',1);
            $player_nums = Yii::$app->request->post('player_nums', 0);
            if(empty($player_nums)) throw new \Exception('参赛人数不能为空',1);
            $season = Yii::$app->request->post('season', 0);
            if(empty($season)) throw new \Exception('赛季不能为空',1);
            $system = Yii::$app->request->post('system', 0);
            if(empty($system)) throw new \Exception('赛制不能为空',1);
            $place_id = Yii::$app->request->post('place_id', 0);
            if(empty($place_id)) throw new \Exception('场地不能为空',1);
            $begin_date = Yii::$app->request->post('begin_date', 0);
            if(empty($begin_date)) throw new \Exception('开始时间不能为空',1);
            $end_date = Yii::$app->request->post('end_date', 0);
            if(empty($end_date)) throw new \Exception('结束时间不能为空',1);
            $close_date = Yii::$app->request->post('close_date', 0);
            if(empty($close_date)) throw new \Exception('报名截止时间不能为空',1);
            $obj = Game::findOne($id);
            if(empty($obj)){
                $obj = new Game();
                $obj->ctime = time();
            }
            $obj->title = $title;
            $obj->intro = $intro;
            $obj->cover_id = $cover_id;
            $obj->player_nums = $player_nums;
            $obj->season = $season;
            $obj->system = $system;
            $obj->place_id = $place_id;
            $obj->begin_date = $begin_date;
            $obj->end_date = $end_date;
            $obj->close_date = $close_date;
            if(!$obj->save()) throw new \Exception(json_encode($obj->getErrors()),1);
            return $this->success(['id'=>$obj->id]);
        }catch (\Exception $e){
            return $this->error($e);
        }
    }
}
